/***************************************************************************
 *   Copyleft 2006-2007 by												   *
 *	 Iwanicki Krzysztof													   *
 *   <gamma.mini.pw.edu.pl/~iwanickik>									   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 ***************************************************************************/
#include "./EllipsoidManager.h"
#include "../../resource.h"

cFloat EllipsoidManager::mA = 1000.0f;
cFloat EllipsoidManager::mB = 600.0f;
cFloat EllipsoidManager::mC = 300.0f;
/**************************************************************
* Constructors                                                *
***************************************************************/

    
/**************************************************************
* Operators                                                   *
***************************************************************/


/**************************************************************
* Other helpful functions                                     *
***************************************************************/
/*virtual*/ cVoid EllipsoidManager::OnCommand(HWND hwnd, WPARAM wParam, LPARAM lParam, Engine3D* engine) {	
	cUShort cmd = LOWORD(wParam);
	
	if (mObject) {
		OnChangeParams(cmd);
		OnTransform(cmd);
	}
}

cVoid EllipsoidManager::OnMouseMove(cPoint point, Engine3D* engine) {
	if (mLBDown) {
		::SetCursor(::LoadCursor(cNull, IDC_SIZEALL));
		mObject->Transform(
			Matrix4D::Translation(Point4D((cFloat)(point.x - mLBDownHit.x), 
							(cFloat) (point.y - mLBDownHit.y), 0.0f)), false);
		mLBDownHit = point;
	} else if (mRBDown) {
		::SetCursor(::LoadCursor(cNull, IDC_SIZENS));
		mObject->Transform(Matrix4D::Scaling(1.0f + (point.y - mRBDownHit.y) / 150.0f));
		mRBDownHit = point;
	}
}

cVoid EllipsoidManager::OnLeftMouseUp(cPoint point, Engine3D* engine) {
	InputManager::OnLeftMouseUp(point, engine);
	::SetCursor(::LoadCursor(cNull, IDC_ARROW));
}

cVoid EllipsoidManager::OnRightMouseUp(cPoint point, Engine3D* engine) {
	InputManager::OnRightMouseUp(point, engine);
	::SetCursor(::LoadCursor(cNull, IDC_ARROW));
}

cVoid EllipsoidManager::OnTransform(cUShort cmd) {
	switch(cmd) {
		case ID_ROTTXA:
			mObject->Transform(Matrix4D::RotationX(-5.0f));
			break;
		case ID_ROTTXQ:
			mObject->Transform(Matrix4D::RotationX(5.0f));
			break;
		case ID_ROTTYS:
			mObject->Transform(Matrix4D::RotationY(-5.0f));
			break;
		case ID_ROTTYW:
			mObject->Transform(Matrix4D::RotationY(5.0f));
			break;
		case ID_ROTTZD:
			mObject->Transform(Matrix4D::RotationZ(-5.0f));
			break;
		case ID_ROTTZE:
			mObject->Transform(Matrix4D::RotationZ(5.0f));
			break;
		case ID_TRANSIZ:
			mObject->Transform(Matrix4D::Translation(Point4D(0.0f, 0.0f, 0.20f)), false);
			break;
		case ID_TRANSDZ:
			mObject->Transform(Matrix4D::Translation(Point4D(0.0f, 0.0f, -0.20f)), false);
			break;
		default:
			break;
	}
}

cVoid EllipsoidManager::OnChangeParams(cUShort cmd) {
	switch(cmd) {
		case ID_ELLIPSOID_INCM:
			((Ellipsoid*) mObject)->ChangeMFactor(0.5f);
			break;
		case ID_ELLIPSOID_DECM:
			((Ellipsoid*) mObject)->ChangeMFactor(-0.5f);
			break;
		case ID_ELLIPSOID_SHOWBB:
			((Ellipsoid*) mObject)->ShowHideBoundingBox();
			break;
		case ID_ELLIPSOID_SHOWRCP:
			((Ellipsoid*) mObject)->ShowHideRayCastingPlane();
			break;
		case ID_ELLIPSOID_DECA:
			EllipsoidManager::mA -= 50.0f;
			((Ellipsoid*) mObject)->ChangeA(EllipsoidManager::mA);
			break;
		case ID_ELLIPSOID_INCA:
			EllipsoidManager::mA += 50.0f;
			((Ellipsoid*) mObject)->ChangeA(EllipsoidManager::mA);
			break;
		case ID_ELLIPSOID_DECB:
			EllipsoidManager::mB -= 50.0f;
			((Ellipsoid*) mObject)->ChangeB(EllipsoidManager::mB);
			break;
		case ID_ELLIPSOID_INCB:
			EllipsoidManager::mB += 50.0f;
			((Ellipsoid*) mObject)->ChangeB(EllipsoidManager::mB);
			break;
		case ID_ELLIPSOID_DECC:
			EllipsoidManager::mC -= 50.0f;
			((Ellipsoid*) mObject)->ChangeC(EllipsoidManager::mC);
			break;
		case ID_ELLIPSOID_INCC:
			EllipsoidManager::mC += 50.0f;
			((Ellipsoid*) mObject)->ChangeC(EllipsoidManager::mC);
			break;
		default:
			break;
	}
	if (EllipsoidManager::mA < 100.0f) {
		EllipsoidManager::mA = 150.0f;
	}
	if (EllipsoidManager::mB < 100.0f) {
		EllipsoidManager::mB = 150.0f;
	}
	if (EllipsoidManager::mC < 100.0f) {
		EllipsoidManager::mC = 150.0f;
	}
}

cBool EllipsoidManager::GetAdaptiveInProgress() {
	return mAdaptiveInProgress;
}

cVoid EllipsoidManager::GetAdaptiveInProgress(cBool bStart) {
	mAdaptiveInProgress = bStart;
}